package com.shangma.cn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shangma.cn.commons.page.PageBean;
import com.shangma.cn.dto.base.impl.BrandDTO;
import com.shangma.cn.entity.base.impl.Brand;
import com.shangma.cn.mapper.BrandMapper;
import com.shangma.cn.query.impl.BrandQuery;
import com.shangma.cn.service.BrandService;
import com.shangma.cn.service.base.impl.BaseServiceImpl;
import com.shangma.cn.transfer.base.impl.BrandTransfer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;


/**
 * 开发人员: 辉哥真球帅
 * 联系方式: zhengzhoudaxuevip@163.com
 * 创建时间: 2021/10/7  16:42
 * 文件含义:
 */
@Service
@RequiredArgsConstructor
public class BrandServiceImpl extends BaseServiceImpl<Brand> implements BrandService {


    public final BrandMapper brandMapper;
    public final BrandTransfer brandTransfer;

    /**
     * 分页条件查询
     * <p>
     * if()
     * select * from brand where brand_name like "%值%" and  create_time between start end end
     *
     * @param brandQuery
     * @return
     */
    @Override
    public PageBean<BrandDTO> searchPage(BrandQuery brandQuery) {
        PageHelper.startPage(brandQuery.getCurrentPage(), brandQuery.getPageSize());
        List<Brand> brands = null;
        if (brandQuery.isQuery()) {
            LambdaQueryWrapper<Brand> lambda = new QueryWrapper<Brand>().lambda();
            if (!StringUtils.isEmpty(brandQuery.getBrandName())){
                lambda.like(Brand::getBrandName, brandQuery.getBrandName());
            }
            if (brandQuery.getStartTime() != null && brandQuery.getEndTime() != null) {
                lambda.between(Brand::getCreateTime, brandQuery.getStartTime(), brandQuery.getEndTime());
            }

            brands = brandMapper.selectList(lambda);
        } else {
            brands = brandMapper.selectList(null);
        }
        PageInfo<Brand> pageInfo = new PageInfo<>(brands);
        long total = pageInfo.getTotal();
        PageBean<BrandDTO> pageBean = new PageBean<>(total, brandTransfer.toDTOS(brands));
        return pageBean;
    }
}
